//
//  CollectViewController.m
//  intelligent
//
//  Created by 甘世清 on 2024/2/10.
//

#import "CollectViewController.h"
#import "CollectTableViewCell.h"
#import "CollectView.h"
@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.zx_navTitle = SQNSLocalString(@"我的收藏");
}
- (void)SQ_UIViewForSelfView{
   
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}

- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:@"chat/collectList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MessageModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:@"chat/collectList" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MessageModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectTableViewCell"];
    if (!cell) {
        cell = [[CollectTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"CollectTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    WS(weakSelf);
    [cell.loveBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_collectPopViewindex:indexPath];
    }];
    [cell.copyBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf copySelectText:indexPath];
    }];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  [CollectTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

-(void)copySelectText:(NSIndexPath *)index{
    MessageModel * model = self.jsonArray[index.row];
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    pab.string = model.content;
    if (pab == nil) {
        [YJProgressHUD showMessage:SQNSLocalString(@"复制失败") inView:self.tableView];
        
    } else {
        [YJProgressHUD showMessage:SQNSLocalString(@"复制成功") inView:self.tableView];
        
    }
}

-(void)SQ_collectPopViewindex:(NSIndexPath*)index{
    
    CollectView * vc = [[CollectView alloc]initWithFrame:CGRectMake(0, 0, 320, 240)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleCenter;
   
    [pop pop];
    LSTPopViewWK(pop)
                 
     [vc.leftBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
         [wk_pop dismiss];
    }];
    WS(weakSelf);
    MessageModel * model = [self.jsonArray objectAtIndex:index.item];
    [vc.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setObject:model.userid forKey:@"id"];
         [dic setObject:@"0" forKey:@"type"];
        [HttpTool post:@"chat/collect" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            [weakSelf.jsonArray removeObject:model];
            [weakSelf.tableView deleteRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationRight];
            [weakSelf.tableView reloadData];
            [wk_pop dismiss];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }];
}




@end
